package com.xkdx.caipiao.setup;

import java.util.HashMap;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.toggle.SettingUtils;
import com.xkdx.caipiao.toggle.ToggleListener;

public class PushActivtiy extends ThreadActivity {
	private ImageView back;
	private LinearLayout layout_seven_color, layout_lotto, layout_array3,
			layout_array5;
	private ToggleButton toggle_seven_color, toggle_lotto, toggle_array3,
			toggle_array5;
	private ImageButton toggleButton_seven_color, toggleButton_lotto,
			toggleButton_array3, toggleButton_array5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_push_caipiao);

		// LinearLayout找到控件
		layout_seven_color = (LinearLayout) findViewById(R.id.layout_seven_color);
		layout_lotto = (LinearLayout) findViewById(R.id.layout_lotto);
		layout_array3 = (LinearLayout) findViewById(R.id.layout_arrange_3);
		layout_array5 = (LinearLayout) findViewById(R.id.layout_arrange_5);

		// Toggle
		toggle_seven_color = (ToggleButton) findViewById(R.id.toggle_seven_color);
		toggle_lotto = (ToggleButton) findViewById(R.id.toggle_lotto);
		toggle_array3 = (ToggleButton) findViewById(R.id.toggle_arrange_3);
		toggle_array5 = (ToggleButton) findViewById(R.id.toggle_arrange_5);

		// ToggleButton
		toggleButton_seven_color = (ImageButton) findViewById(R.id.toggleButton_seven_color);
		toggleButton_lotto = (ImageButton) findViewById(R.id.toggleButton_lotto);
		toggleButton_array3 = (ImageButton) findViewById(R.id.toggleButton_arrange_3);
		toggleButton_array5 = (ImageButton) findViewById(R.id.toggleButton_arrange_5);

		init();
		initViews();
		setListeners();
	}

	private void init() {
		back = (ImageView) findViewById(R.id.push_back);

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initViews() {
		// 是否七星彩，获取SharePerference保存的用户配置
		boolean isSevenColor = SettingUtils.get(this, SettingUtils.SEVEN_COLOR,
				true);
		setState(isSevenColor, toggle_seven_color, toggleButton_seven_color,
				R.id.toggle_seven_color, R.id.toggleButton_seven_color);

		boolean isLOTTO = SettingUtils.get(this, SettingUtils.LOTTO, true);
		setState(isLOTTO, toggle_lotto, toggleButton_lotto, R.id.toggle_lotto,
				R.id.toggleButton_lotto);

		// 是否排列3，获取SharePerference保存的用户配置
		boolean isArray3 = SettingUtils.get(this, SettingUtils.ARRAY_3, true);

		setState(isArray3, toggle_array3, toggleButton_array3,
				R.id.toggle_arrange_3, R.id.toggleButton_arrange_3);

		// 是否排列5，获取SharePerference保存的用户配置
		boolean isArray5 = SettingUtils.get(this, SettingUtils.ARRAY_5, true);
		setState(isArray5, toggle_array5, toggleButton_array5,
				R.id.toggle_arrange_5, R.id.toggleButton_arrange_5);

	}

	private void setState(boolean isPush, ToggleButton togglebtn,
			ImageButton im_toggle, int togglebtnid, int im_toggleid) {

		togglebtn.setChecked(isPush);

		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) im_toggle
				.getLayoutParams();
		if (isPush) { // 如果是七星彩
			// 调整位置
			params.addRule(RelativeLayout.ALIGN_RIGHT, -1);
			params.addRule(RelativeLayout.ALIGN_LEFT, im_toggleid);
			im_toggle.setLayoutParams(params);
			im_toggle.setImageResource(R.drawable.progress_thumb_selector_caipiao);
			togglebtn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
		} else {
			// 调整位置
			params.addRule(RelativeLayout.ALIGN_RIGHT, togglebtnid);
			params.addRule(RelativeLayout.ALIGN_LEFT, -1);
			im_toggle.setLayoutParams(params);
			im_toggle.setImageResource(R.drawable.progress_thumb_selector_caipiao);
			togglebtn.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		}

	}

	private void setListeners() {
		toggle_seven_color.setOnCheckedChangeListener(new ToggleListener(this,
				IConstants.SEVEN_COLOR, toggle_seven_color,
				toggleButton_seven_color));
		toggle_lotto.setOnCheckedChangeListener(new ToggleListener(this,
				IConstants.LOTTO, toggle_lotto, toggleButton_lotto));

		toggle_array3.setOnCheckedChangeListener(new ToggleListener(this,
				IConstants.ARRANGE_3, toggle_array3, toggleButton_array3));
		toggle_array5.setOnCheckedChangeListener(new ToggleListener(this,
				IConstants.ARRANGE_5, toggle_array5, toggleButton_array5));

		// UI事件，按钮点击事件
		OnClickListener clickToToggleListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				toggle_seven_color.toggle();
			}
		};

		toggleButton_seven_color.setOnClickListener(clickToToggleListener);
		layout_seven_color.setOnClickListener(clickToToggleListener);

		// UI事件，按钮点击事件
		OnClickListener clickToToggleAutostartListener = new OnClickListener() {
			public void onClick(View v) {
				toggle_lotto.toggle();
			}
		};
		toggleButton_lotto.setOnClickListener(clickToToggleAutostartListener);
		layout_lotto.setOnClickListener(clickToToggleAutostartListener);

		// UI事件，按钮点击事件
		OnClickListener clickToToggleArray3Listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				toggle_array3.toggle();
			}
		};

		toggleButton_array3.setOnClickListener(clickToToggleArray3Listener);
		layout_array3.setOnClickListener(clickToToggleArray3Listener);

		// UI事件，按钮点击事件
		OnClickListener clickToToggleArray5Listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				toggle_array5.toggle();
			}
		};

		toggleButton_array5.setOnClickListener(clickToToggleArray5Listener);
		layout_array5.setOnClickListener(clickToToggleArray5Listener);

	}

	@Override
	public void startThread() {

	}

	public void onBackClick(View view) {
		finish();
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {

	}
}
